﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.CRM.AppCode.Lib;

namespace mojoPortal.Web.RS.CRM.Controls
{
    public partial class AgentHeaderControl : RSBaseUserControl
    {
        public int AgentID {
            get {
                int id = hfAgentId.Value == "" ? 0 : Convert.ToInt32(hfAgentId.Value);
                return id;
            }
        }
        public int CompanyID
        {
            get
            {
                int id = hfCompanyId.Value == "" ? 0 : Convert.ToInt32(hfCompanyId.Value);
                return id;
            }
        }
        public string AgentFirstName
        {
            get
            {
                return hfFirstName.Value;
            }
        }
        public string AgentLastName
        {
            get
            {
                return hfLastName.Value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {

            }
        }

        protected void lnkReportsTo_Click(object sender, EventArgs e)
        {
            Session["SELECTED_AGENTID_IS_MY_DOWNLINE_LEAD"] = Convert.ToInt32(hfReportsTo.Value);
            Response.Redirect(Request.RawUrl);
        }

        public bool LoadAgentData(int companyid, int employeeid)
        {
            hfCompanyId.Value = companyid.ToString();

            bool bHasValue = false;

            String strSQL = "select * from rs_tblEmployee where EmployeeID=@EmployeeID and companyid=@companyid";
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(strSQL, myConnection);
            SqlDataReader myDataReader;

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@employeeid";
            param.Value = employeeid;
            myCommand.Parameters.Add(param);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            myConnection.Open();
            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (myDataReader.HasRows)
            {
                myDataReader.Read();
                lblAgentName.Text = myDataReader["FirstName"].ToString() + " " + myDataReader["LastName"].ToString();
                hfAgentId.Value = myDataReader["EmployeeId"].ToString();
                hfFirstName.Value = myDataReader["FirstName"].ToString();
                hfLastName.Value = myDataReader["LastName"].ToString();

                int reportsto = Convert.ToInt32(myDataReader["ReportsTo"].ToString());
                lnkUplineLeader.Text = RSCRMCommonFunctions.GetEmployeeNameByID(companyid, reportsto);
                lblUplineLeader.Text = lnkUplineLeader.Text;
                hfReportsTo.Value = reportsto.ToString();
                RSUserIdentity userIdentity = GetRSUserIdentity();
                if (userIdentity.RSReportsTo == reportsto || userIdentity.RSEmployeeId == int.Parse(hfAgentId.Value))
                {
                    lnkUplineLeader.Visible = false;
                    lblUplineLeader.Visible = true;
                }
            }

            myConnection.Close();
            return bHasValue;
        }

    }
}